

(function(j, k, f) {
    var c = mobiscroll,
        a = c.$,
        b = a.extend,
        h = c.util,
        d = h.datetime,
        g = d.adjustedDate,
        e = c.presets.scroller,
        i = {
            labelsShort: ['Yrs', 'Mths', 'Days', 'Hrs', 'Mins', 'Secs'],
            eventText: 'event',
            eventsText: 'events'
        };
    c.presetShort('eventcalendar');
    e.eventcalendar = function(h) {
        function J(b) {
            if (b) {
                if (x[b]) {
                    return x[b];
                }
                var c = a('<div style="background-color:' + b + ';"></div>').appendTo('body'),
                    f = j.getComputedStyle ? getComputedStyle(c[0]) : c[0].style,
                    d = f.backgroundColor.replace(/rgb|rgba|\(|\)|\s/g, '').split(','),
                    g = d[0] * 0.299 + d[1] * 0.587 + d[2] * 0.114,
                    e = g > 130 ? '#000' : '#fff';
                c.remove();
                x[b] = e;
                return e;
            }
        }

        function K(g) {
            var a = k.labelsShort,
                c = Math.abs(g) / 1000,
                d = c / 60,
                e = d / 60,
                b = e / 24,
                f = b / 365;
            return c < 45 && Math.round(c) + ' ' + a[5].toLowerCase() || d < 45 && Math.round(d) + ' ' + a[4].toLowerCase() || e < 24 && Math.round(e) + ' ' + a[3].toLowerCase() || b < 30 && Math.round(b) + ' ' + a[2].toLowerCase() || b < 365 && Math.round(b / 30) + ' ' + a[1].toLowerCase() || Math.round(f) + ' ' + a[0].toLowerCase();
        }

        function E(a) {
            return a.sort(function(a, b) {
                var c = a.d || a.start,
                    d = b.d || b.start,
                    e = !c.getTime ? 0 : a.start && a.end && a.start.toDateString() !== a.end.toDateString() ? 1 : c.getTime(),
                    f = !d.getTime ? 0 : b.start && b.end && b.start.toDateString() !== b.end.toDateString() ? 1 : d.getTime();
                return e - f;
            });
        }

        function F(b) {
            var e, f = a('.mbsc-cal-c', o)[0].offsetHeight,
                g = b[0].offsetHeight,
                h = b[0].offsetWidth,
                d = b.offset().top - a('.mbsc-cal-c', o).offset().top,
                c = b.closest('.mbsc-cal-row').index() < 2;
            e = l.addClass('mbsc-cal-events-t').css({
                top: c ? d + g : '0',
                bottom: c ? '0' : f - d
            }).addClass('mbsc-cal-events-v').height();
            l.css(c ? 'bottom' : 'top', 'auto').removeClass('mbsc-cal-events-t');
            w.css('max-height', e);
            r.refresh();
            r.scroll(0);
            if (c) {
                l.addClass('mbsc-cal-events-b');
            } else {
                l.removeClass('mbsc-cal-events-b');
            }
            a('.mbsc-cal-events-arr', l).css('left', b.offset().left - l.offset().left + h / 2);
        }

        function I(o, m) {
            var b = q[o];
            if (b) {
                var g, e, j, c, i, p, f = '<ul class="mbsc-cal-event-list">';
                M = 0;
                n = m;
                m.addClass(D).find('.mbsc-cal-day-i').addClass(C);
                if (m.hasClass(v)) {
                    m.attr('data-hl', 'true').removeClass(v);
                }
                E(b);
                a.each(b, function(b, a) {
                    c = a.d || a.start;
                    i = a.start && a.end && a.start.toDateString() !== a.end.toDateString();
                    j = a.color;
                    p = J(j);
                    g = '';
                    e = '';
                    if (c.getTime) {
                        g = d.formatDate((i ? 'MM d yy ' : '') + k.timeFormat, c);
                    }
                    if (a.end) {
                        e = d.formatDate((i ? 'MM d yy ' : '') + k.timeFormat, a.end);
                    }
                    f += '<li role="button" aria-label="' + a.text + (g ? ', ' + k.fromText + ' ' + g : '') + (e ? ', ' + k.toText + ' ' + e : '') + '" class="mbsc-cal-event">' + '<div class="mbsc-cal-event-color" style="' + (j ? 'background:' + j + ';' : '') + '"></div>' + '<div class="mbsc-cal-event-text">' + (c.getTime && !i ? '<div class="mbsc-cal-event-time">' + d.formatDate(k.timeFormat, c) + '</div>' : '') + a.text + '</div>' + (a.start && a.end ? '<div class="mbsc-cal-event-dur">' + K(a.end - a.start) + '</div>' : '') + '</li>';
                });
                f += '</ul>';
                B.html(f);
                h.trigger('onEventBubbleShow', {
                    target: n[0],
                    eventList: l[0]
                });
                F(n);
                h.tap(a('.mbsc-cal-event', B), function(c) {
                    if (!r.scrolled) {
                        h.trigger('onEventSelect', {
                            domEvent: c,
                            event: b[a(this).index()],
                            date: o
                        });
                    }
                });
                y = true;
            }
        }

        function t() {
            if (l) {
                l.removeClass('mbsc-cal-events-v');
            }
            if (n) {
                n.removeClass(D).find('.mbsc-cal-day-i').removeClass(C);
                if (n.attr('data-hl')) {
                    n.removeAttr('data-hl').addClass(v);
                }
            }
            y = false;
        }

        function A() {
            t();
            h.redraw();
        }

        function L(a) {
            return g(a.getFullYear(), a.getMonth(), a.getDate());
        }
        var H, o, l, n, q, r, w, B, y, M, z, s, x = {},
            G = b({}, h.settings),
            k = b(h.settings, i, G),
            D = 'mbsc-cal-day-sel mbsc-cal-day-ev',
            v = 'mbsc-cal-day-hl',
            C = k.activeClass || '',
            p = k.showEventCount,
            u = 0,
            m = b(true, [], k.data);
        z = e.calbase.call(this, h);
        H = b({}, z);
        a.each(m, function(b, a) {
            if (a._id === f) {
                a._id = u++;
            }
        });
        h.onGenMonth = function(a, b) {
            q = h.prepareObj(m, a, b);
        };
        h.getDayProps = function(g) {
            var c, b = q[g] ? q[g] : false,
                d = b ? q[g].length + ' ' + (q[g].length > 1 ? k.eventsText : k.eventText) : 0,
                h = b && b[0] && b[0].color,
                i = p && d ? J(h) : '',
                e = '',
                f = '';
            if (b) {
                for (c = 0; c < b.length; c++) {
                    if (b[c].icon) {
                        e += '<span class="mbsc-ic mbsc-ic-' + b[c].icon + '"' + (b[c].text ? '' : b[c].color ? ' style="color:' + b[c].color + ';"' : '') + '></span>\n';
                    }
                }
                f = '<div class="mbsc-cal-day-m"><div class="mbsc-cal-day-m-t">';
                for (c = 0; c < b.length; c++) {
                    f += '<div class="mbsc-cal-day-m-c"' + (b[c].color ? ' style="background:' + b[c].color + ';"' : '') + '></div>';
                }
                f += '</div></div>';
            }
            return {
                marked: b,
                selected: false,
                cssClass: b ? 'mbsc-cal-day-marked' : '',
                ariaLabel: p ? d : '',
                markup: p && d ? '<div class="mbsc-cal-day-txt-c"><div class="mbsc-cal-day-txt" title="' + a('<div>' + d + '</div>').text() + '"' + (h ? ' style="background:' + h + ';color:' + i + ';text-shadow:none;"' : '') + '>' + e + d + '</div></div>' : p && e ? '<div class="mbsc-cal-day-ic-c">' + e + '</div>' : b ? f : ''
            };
        };
        h.addEvent = function(c) {
            var d = [];
            c = b(true, [], a.isArray(c) ? c : [c]);
            a.each(c, function(b, a) {
                if (a._id === f) {
                    a._id = u++;
                }
                m.push(a);
                d.push(a._id);
            });
            A();
            return d;
        };
        h.removeEvent = function(b) {
            b = a.isArray(b) ? b : [b];
            a.each(b, function(c, b) {
                a.each(m, function(a, c) {
                    if (c._id === b) {
                        m.splice(a, 1);
                        return false;
                    }
                });
            });
            A();
        };
        h.getEvents = function(a) {
            var c;
            if (a) {
                a.setHours(0, 0, 0, 0);
                c = h.prepareObj(m, a.getFullYear(), a.getMonth());
                return c[a] ? E(c[a]) : [];
            }
            return b(true, [], m);
        };
        h.setEvents = function(d) {
            var c = [];
            m = b(true, [], d);
            a.each(m, function(b, a) {
                if (a._id === f) {
                    a._id = u++;
                }
                c.push(a._id);
            });
            A();
            return c;
        };
        b(z, {
            highlight: false,
            outerMonthChange: false,
            headerText: false,
            buttons: k.display !== 'inline' ? ['cancel'] : k.buttons,
            onMarkupReady: function(b) {
                H.onMarkupReady.call(this, b);
                o = a(b.target);
                if (p) {
                    a('.mbsc-cal', o).addClass('mbsc-cal-ev');
                }
                o.addClass('mbsc-cal-em');
                l = a('<div class="mbsc-cal-events ' + (k.eventBubbleClass || '') + '"><div class="mbsc-cal-events-arr"></div><div class="mbsc-cal-events-i"><div class="mbsc-cal-events-sc"></div></div></div>').appendTo(a('.mbsc-cal-c', o));
                w = a('.mbsc-cal-events-i', l);
                B = a('.mbsc-cal-events-sc', l);
                r = new c.classes.ScrollView(w[0]);
                y = false;
                h.tap(w, function() {
                    if (!r.scrolled) {
                        t();
                    }
                });
            },
            onMonthChange: function() {
                t();
            },
            onSelectShow: function() {
                t();
            },
            onMonthLoaded: function() {
                if (s) {
                    I(s.d, a('.mbsc-cal-day-v[data-full="' + s.full + '"]:not(.mbsc-cal-day-diff)', o));
                    s = false;
                }
            },
            onDayChange: function(d) {
                var e = d.date,
                    c = L(e),
                    b = a(d.target);
                t();
                if (!b.hasClass('mbsc-cal-day-ev')) {
                    setTimeout(function() {
                        if (h.changing) {
                            s = {
                                d: c,
                                full: b.attr('data-full')
                            };
                        } else {
                            I(c, b);
                        }
                    }, 10);
                }
                return false;
            },
            onCalResize: function() {
                if (y) {
                    F(n);
                }
            }
        });
        return z;
    };
}(window, document));